package mrusanov.ui.menu

import javax.swing._
import mrusanov.localization.Localization
import java.awt.GridLayout
import mrusanov.util.swing.{ Event, EventConsumer }
import java.awt.event.{ ActionEvent, ActionListener }
import mrusanov.ui.newgame.NewGamePanel
import mrusanov.ui.management.{ TeamRosterPanel, FinancePanel, CalendarPanel }
import java.util.Date
import mrusanov.team.Club
import mrusanov.season.WorldSeason
import mrusanov.ui.management.buildings.BuildingsPanel
import mrusanov.ui.management.staff.StaffPanel
import mrusanov.ui.{ CentralPanel, PanelBuilder }

class MenuBuilder(panel: PanelBuilder, consumer: EventConsumer[JPanel]) {

	def buildMenuForGameNotStarted: JMenuBar = {
		val bar = new JMenuBar
		bar.add(buildGameMenu)
		bar
	}

	def buildMenuForBattle: JMenuBar = null

	def buildMenuForGameStarted: JMenuBar = {
		val bar = new JMenuBar
		bar.add(buildGameMenu)
		bar.add(buildClubMenu)
		bar.add(buildTeamMenu)
		bar
	}

	def buildQuickMenuForGameNotStarted: JPanel = new JPanel()

	def buildQuickMenuForBattle: JPanel = new JPanel()

	def buildQuickMenuForGameStarted: JPanel = {
		val quickPanel = new JPanel
		quickPanel.setLayout(new GridLayout(1, 0, 20, 20))
		val firstTeamButton = new JButton(Localization("menu.firstTeam"))
		firstTeamButton.addActionListener(toListener(panel.firstTeam))
		quickPanel.add(firstTeamButton)
		quickPanel.add(panel.nextEvent)

		quickPanel
	}

	private def buildGameMenu: JMenu = {
		val mainMenu = new JMenu(Localization("menu.game"))
		val newGame = new JMenuItem(Localization("menu.newGame"))
		newGame.addActionListener(toListener(panel.newGame))
		val loadGame = new JMenuItem(Localization("menu.loadGame"))
		loadGame.addActionListener(toListener(panel.loadGame))
		// TODO check whether can be saved
		val saveGame = new JMenuItem(Localization("menu.saveGame"))
		saveGame.addActionListener(toListener(panel.saveGame))
		val exitGame = new JMenuItem(Localization("menu.exitGame"))
		exitGame.addActionListener(toListener(exitGameFunc()))

		val items = List(newGame, loadGame, saveGame, exitGame)
		items foreach (mainMenu.add(_))

		mainMenu
	}

	private def buildClubMenu: JMenu = {
		val mainMenu = new JMenu(Localization("menu.club"))

		val results = new JMenuItem(Localization("menu.results"))
		results.addActionListener(toListener(panel.results))
		val calendar = new JMenuItem(Localization("menu.calendar"))
		calendar.addActionListener(toListener(panel.calendar))
		val infra = new JMenuItem(Localization("menu.infrastructure"))
		infra.addActionListener(toListener(panel.infra))
		val finance = new JMenuItem(Localization("menu.finance"))
		finance.addActionListener(toListener(panel.finance))
		val staff = new JMenuItem(Localization("menu.staff"))
		staff.addActionListener(toListener(panel.staff))
		val items = List(calendar, results, infra, finance, staff)
		items foreach (mainMenu.add(_))

		mainMenu
	}

	private def buildTeamMenu: JMenu = {
		val mainMenu = new JMenu(Localization("menu.team"))

		val firstTeam = new JMenuItem(Localization("menu.firstTeam"))
		firstTeam.addActionListener(toListener(panel.firstTeam))
		val secondTeam = new JMenuItem(Localization("menu.secondTeam"))
		secondTeam.addActionListener(toListener(panel.secondTeam))
		val juniorTeam = new JMenuItem(Localization("menu.juniorTeam"))
		juniorTeam.addActionListener(toListener(panel.juniorTeam))
		val transfers = new JMenuItem(Localization("menu.transfers"))
		transfers.addActionListener(toListener(panel.transfers))
		val items = List(firstTeam, secondTeam, juniorTeam, transfers)
		items foreach (mainMenu.add(_))

		mainMenu
	}

	private def toListener(func: => CentralPanel) = new ActionListener {
		def actionPerformed(e: ActionEvent) {
			consumer.consumeEvent(new Event(func))
		}
	}

	private def exitGameFunc() = {
		System.exit(0)

		// TODO remove exception and add "Would you like to save game?"
		throw new IllegalArgumentException("This exception must be removed before release")
	}
}
